import BasePanel from "./BasePanel";
import GamePropUi from "../UI/GamePanel/GamePropUi";
import LevelBaseControl from "../LevelCommon/LevelBaseControl";
import { DialogData } from "../Data/DialogData";
import DialogPanel from "./DialogPanel";
import { LevelData } from "../Data/LevelData";
import CommonConstant from "../../constant/CommonConstant";
import Sound from "../../constant/SoundNameConstant";
import LevelInit1 from "../LevelInit/LevelInit1";
import LevelInit2 from "../LevelInit/LevelInit2";
import LevelInit3 from "../LevelInit/LevelInit3";
import LevelInit4 from "../LevelInit/LevelInit4";
import LevelInit5 from "../LevelInit/LevelInit5";
import LevelInit6 from "../LevelInit/LevelInit6";
import LevelInit7 from "../LevelInit/LevelInit7";
import LevelInit8 from "../LevelInit/LevelInit8";
import LevelInit9 from "../LevelInit/LevelInit9";
import LevelInit10 from "../LevelInit/LevelInit10";
import { utils } from "../../utils/CommonUtil";
import CameraControl from "../Camera/CameraControl";
import CameraRayLogic from "../Camera/CameraRayLogic";
import EnumData from "../../Enum/EnumData";
import PropMessagePanel from "./PropMessagePanel";
import EventNameList from "../../constant/EventNameList";
import { GamePropData } from "../Data/GamePropData";
import CompositePanel from "./CompositePanel";

export default class GamePanel extends BasePanel {

    constructor(id) {
        super();
        this.m_nameID = id;
    }

    /** 相机 移动回退按钮*/
    private cameraBackBtn: fairygui.GButton;

    /** 当前点击的道具ui*/
    public curClickPropUi: GamePropUi;

    private curLevelControl: LevelBaseControl;

    /** 提示按钮*/
    private hintBtn: fairygui.GButton;

    /** 当前的道具ui*/
    private curPropUiList: Array<GamePropUi>;

    /** 对话面板*/
    private dialogPanel: DialogPanel;

    /** 纪念品ui*/
    private memorialUis: Array<fairygui.GLoader>;


    /** 道具表 控制按钮*/
    private subBtn: fairygui.GButton;

    private addBtn: fairygui.GButton;

    //獲得的道具列表
    private gamePropList: fairygui.GList;

    private gamePropListRoot:fairygui.GGroup;

    //3D游戏场景
    private m_scene3D: Laya.Scene3D;

    public currentLevelData: LevelData;

    //场景根节点
    private levelRoot: Laya.Sprite3D;

    //触摸旋转图片
    private rotateCameraImage: fairygui.GImage;

    //遮罩图片，打开禁止操作
    private MaskImage:fairygui.GImage;

    public CameraControl: CameraControl;

    private cameraControlPrefab:Laya.Sprite3D;

    private pauseBtn:fairygui.GButton;

    public get CurrentScene3D(): Laya.Scene3D {
        return this.m_scene3D;
    }

    public get cameraTouchImage(): fairygui.GImage {
        return this.rotateCameraImage;
    }

    public EnterAfter(scene3D: Laya.Scene3D, levelData: LevelData) {
        this.m_scene3D = scene3D;
        this.currentLevelData = levelData;
        this.memorialUis = new Array<fairygui.GLoader>();
        this.curPropUiList = new Array<GamePropUi>();
        this.addChild(scene3D); //添加游戏场景到Panel下
        this.InitFui();
        this.LoadCamereRoot();
    }

    //先加载摄像机
    private LoadCamereRoot(): void {
        /** 生成相机*/
        let rotateCameraCache = window.gameFacade.SceneMng.GamePropObjPrefabRoot.GetRotateCameraCache();
        if( rotateCameraCache == null){
            window.gameFacade.ResourceMng.LoadSprite3D<Laya.Sprite3D>(
                CommonConstant._cameraRotatePrefab,
                this.onCameraControlPrefabLoadSucc, this);
        }else{
            this.cameraControlPrefab = rotateCameraCache;
            this.levelRoot = this.m_scene3D.getChildByName("Level" + this.currentLevelData.levelNum) as Laya.Sprite3D;
            this.levelRoot.addChild(this.cameraControlPrefab);
            this.CameraControl = utils.CommonUtils.CustomAddComponent(this.cameraControlPrefab, CameraControl) as CameraControl;
            this.InitSceneComponents(this.m_scene3D, this.currentLevelData);
        }
    }

    //摄像机加载完成
    private onCameraControlPrefabLoadSucc(cameraControlPrefab: Laya.Sprite3D): void {
        console.log("onCameraControlPrefabLoadSucc");
        this.cameraControlPrefab = cameraControlPrefab.getChildAt(0) as Laya.Sprite3D;
        this.levelRoot = this.m_scene3D.getChildByName("Level" + this.currentLevelData.levelNum) as Laya.Sprite3D;
        this.levelRoot.addChild(this.cameraControlPrefab);
        this.CameraControl = utils.CommonUtils.CustomAddComponent(this.cameraControlPrefab, CameraControl) as CameraControl;

        let camera = this.CameraControl.MainCamera;
        camera.addComponent(CameraRayLogic);

        this.InitSceneComponents(this.m_scene3D, this.currentLevelData);
    }

    //這邊加載LevelControl 和 LevelObjBase
    private InitSceneComponents(scene3D: Laya.Scene3D, levelData: LevelData) {
        switch (levelData.levelNum) {
            case 1: {
                let levelInit = new LevelInit1()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 2: {
                let levelInit = new LevelInit2()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 3: {
                let levelInit = new LevelInit3()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 4: {
                let levelInit = new LevelInit4()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 5: {
                let levelInit = new LevelInit5()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 6: {
                let levelInit = new LevelInit6()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 7: {
                let levelInit = new LevelInit7()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 8: {
                let levelInit = new LevelInit8()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 9: {
                let levelInit = new LevelInit9()
                levelInit.startInit(scene3D, levelData);
                break;
            }
            case 10: {
                let levelInit = new LevelInit10()
                levelInit.startInit(scene3D, levelData);
                break;
            }

            default:
                {
                    let levelInit = new LevelInit1()
                    levelInit.startInit(scene3D, levelData);
                    break;
                }
        }
    }

    private InitFui() {
        fairygui.UIPackage.addPackage(CommonConstant._fuiGameUIPanelPath.Package);
        fairygui.UIPackage.addPackage(CommonConstant._fuiGamePropAtlasPath.Package);
        this.m_fui = fairygui.UIPackage.createObject("GameUIPanel", "GameUIPanel").asCom;
        window.gameFacade.SceneMng.AddFUI(this.m_fui);

        this.cameraBackBtn = this.m_fui.getChild("cameraBack").asButton;
        this.cameraBackBtn.onClick(this, this.onCameraBackBtnClicked);
        this.cameraBackBtn.visible = false;

        window.gameFacade.EventMng.add(EventNameList.RefreshBackCameraBtn, this, this.RefreshBackCameraBtn)

        this.gamePropListRoot = this.m_fui.getChild("gamePropListRoot").asGroup;
        this.gamePropList = this.m_fui.getChild("gamePropList").asList;
        this.gamePropList.removeChildrenToPool();
        this.gamePropList.foldInvisibleItems = true;

        let dialogPanelGroup = this.m_fui.getChild("dialogPanel").asGroup;
        this.dialogPanel = utils.CommonUtils.CustomAddComponent(dialogPanelGroup.displayObject, DialogPanel);
        this.dialogPanel.SetData(this.m_fui);

        // this.memorialUis
        let memorial1 = this.m_fui.getChild("memorial1").asLoader;
        let memorial2 = this.m_fui.getChild("memorial2").asLoader;
        let memorial3 = this.m_fui.getChild("memorial3").asLoader;
        this.memorialUis.push(memorial1);
        this.memorialUis.push(memorial2);
        this.memorialUis.push(memorial3);

        this.subBtn = this.m_fui.getChild("subBtn").asButton;
        this.subBtn.onClick(this, this.OnClickListBtn);
        this.addBtn = this.m_fui.getChild("addBtn").asButton;
        this.addBtn.onClick(this, this.OnClickListBtn);

        this.m_fui.getChild("hintBtn").onClick(this, this.OnClickHintBtn);
        this.pauseBtn = this.m_fui.getChild("pauseBtn").asButton;
        this.pauseBtn.onClick(this, this.OnClickPauseBtn)
        this.rotateCameraImage = this.m_fui.getChild("touchImage") as fairygui.GImage;
        this.rotateCameraImage.displayObject.name = CommonConstant._touchImageName;

        this.MaskImage =  this.m_fui.getChild("MaskImage").asImage;
        this.MaskImage.visible = false;

        this.sound();
        super.Show();
    }

    private OnClickHintBtn() {
        window.gameFacade.SceneMng.Create(EnumData.EnumPanelType.HintPanel);
    }

    /** 刷新相机回退按钮*/
    private RefreshBackCameraBtn(obj: any): void {
        let curCamera: CameraControl = this.CameraControl;
        if (curCamera) {
            /** 相机移动栈 不小于0 显示回退按钮*/
            if (curCamera.curMoveStack.Count > 0) {
                this.cameraBackBtn.visible = true;
                this.pauseBtn.visible = false;
            }
            else {
                this.cameraBackBtn.visible = false;
                this.pauseBtn.visible = true;
            }
        }
        else {
            this.cameraBackBtn.visible = false;
            this.pauseBtn.visible = true;
        }
    }

    private onCameraBackBtnClicked() {
        if (this.CameraControl) {
            this.CameraControl.PopMoveStack();
        }
    }

    private OnClickPauseBtn() {
        window.gameFacade.SceneMng.EnterMainScene(null);
    }

    public ClickPropUi(propUi: GamePropUi): void {
        if (this.curClickPropUi != null) {
            /** 点了已选中的 取消选中*/
            if (this.curClickPropUi == propUi) {
                this.curClickPropUi.SetSelect(false);
                this.curClickPropUi = null;
            }
            /** 点击了另外一个道具 打开合成面板*/
            else {
                let materailPropData: Array<GamePropData> = Array<GamePropData>()
                materailPropData.push(this.curClickPropUi.curData);
                materailPropData.push(propUi.curData);
                let compositePanel = window.gameFacade.SceneMng.Create(EnumData.EnumPanelType.CompositePanel,materailPropData) as CompositePanel;

                /** 要合成的道具*/
               
                compositePanel.SetData(materailPropData);
            }
        }
        else {
            propUi.SetSelect(true);
            this.curClickPropUi = propUi;
        }
    }

    /** 获得了新的道具 并显示*/
    public GetNewProp(newPropData: GamePropData): void {
        let gamePropItemCom: fairygui.GComponent = this.gamePropList.addItemFromPool().asCom;
        /** 根据获得的道具 生成对应的道具ui*/
        let propUi: GamePropUi = utils.CommonUtils.CustomAddComponent(gamePropItemCom.displayObject, GamePropUi) as GamePropUi;
        propUi.SetData(gamePropItemCom, newPropData, this);
        this.curPropUiList.push(propUi);
    }

    /** 使用道具*/
    public UseProp(usePropDatas: Array<GamePropData>): void {
        let length = usePropDatas.length;
        for (let i = 0; i < length; i++) {
            let usePropData: GamePropData = usePropDatas[i];
            /** 获得对应的道具ui*/
            let propUi: GamePropUi = this.GetPropUiByData(usePropData);
            if (propUi != null) {
                if (this.curClickPropUi == propUi) {
                    this.curClickPropUi = null;
                }
                /** 从list里移除这个道具*/
                let index = this.curPropUiList.indexOf(propUi);
                if (index != -1) {
                    this.gamePropList.removeChildAt(index);
                    this.curPropUiList.splice(index, 1);
                }

                /** 销毁这个道具ui*/
                propUi.owner.destroy();
            }
        }
    }

    /** 找到道具ui*/
    private GetPropUiByData(propData: GamePropData): GamePropUi {
        let length = this.curPropUiList.length;
        for (let i = 0; i < length; i++) {
            let propUi: GamePropUi = this.curPropUiList[i];
            if (propUi.curData == propData) {
                return propUi;
            }
        }
        return null;
    }

    /** 使用当前选中的道具  返回是否成功*/
    public UseCurProp(): boolean {
        if (this.curClickPropUi == null) {
            console.log("现在没有选中的道具  无法使用！！！！！！");
            return false;
        }
        else {
            /** 从list里移除这个道具*/
            let index = this.curPropUiList.indexOf(this.curClickPropUi);
            if (index != -1) {
                this.gamePropList.removeChildAt(index);
                this.curPropUiList.splice(index, 1);
            }

            /** 销毁这个道具ui*/
            this.curClickPropUi.owner.destroy();
            /** 设置当前选中的道具 为空*/
            this.curClickPropUi = null;
            return true;
        }
    }

    /** 打字机*/
    public TypeDialog(dialogDatas: Array<DialogData>): void {
        this.dialogPanel.StartType(dialogDatas);
    }

    /** 刷新纪念品ui*/
    public RefreshMemorial(num: number): void {
        let length: number = this.memorialUis.length;
        for (let i = 0; i < length; i++) {
            if (i < num) {
                window.gameFacade.AtlasMng.SetSpriteByName(this.memorialUis[i], "GameUIPanel", "memorialGetInGame");
            }
            else {
                window.gameFacade.AtlasMng.SetSpriteByName(this.memorialUis[i], "GameUIPanel", "memorialNoGetInGame");
            }
        }
    }

    private OnClickListBtn() {
        this.gamePropListRoot.visible = !this.gamePropListRoot.visible;
        this.subBtn.visible = this.gamePropListRoot.visible;
        this.addBtn.visible = !this.gamePropListRoot.visible;
    }

    public ShowMaskImageAndAutoHide(time:number, caller,callback){
        this.MaskImage.visible = true;
        Laya.timer.once(time,this,function(){
            this.MaskImage.visible = false;
            if(callback != null){
                callback.call(caller);
            }
        })
    }

    private sound(): void {
        window.gameFacade.SoundMng.playMusic(Sound.soundBgArr.BGM);
    }

    public ExitGameScene() {
        window.gameFacade.SceneMng.GamePropObjScene.addChild(window.gameFacade.SceneMng.GamePropObjPrefabRoot.owner);
        window.gameFacade.SceneMng.GamePropObjScene.addChild(this.cameraControlPrefab);
        window.gameFacade.SceneMng.GamePropObjPrefabRoot.SetRotateCameraCache(this.cameraControlPrefab)
        window.gameFacade.ResourceMng.ReleaseFuiResByGroupName(CommonConstant._groupName_fuiRes);
        this.m_scene3D.destroy(true);
        Laya.loader.clearRes(CommonConstant.GetSceneNameByLevelData(this.currentLevelData));
    }

    onDestroy() {
        window.gameFacade.EventMng.remove(EventNameList.RefreshBackCameraBtn, this, this.RefreshBackCameraBtn)
    }
}